/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2007 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
aLONG with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#include "commonheaders.h"
#include "reminder.h"

#include "ExtraServices.h"

namespace MenuItems {

enum ECascadeType {
	MCAS_DISABLED = 0,
	MCAS_EXIMPORT = 1,
	MCAS_ALL = 2,
	MCAS_NOTINITIATED = 3
};

/**
 * name:	RebuildContact
 * desc:	This function rebuilds the contactmenu. If genmenu is installed and user selected to
 *			cascade menus, a root menu item is created which holds the popup for all the other items.
 * params:	none
 * return:	nothing
 **/
VOID RebuildContact()
{
	static INT mhRoot = -1;
	BOOLEAN bVCardEnabled, 
			bCascade = FALSE, 
			bRootChanged = FALSE;
	CLISTMENUITEM mi;

	NServices::NSendRecvEMail::RebuildMenu();
	NServices::NViewHomepage::RebuildMenu();

	// set all bytes 0 to avoid problems
	ZeroMemory(&mi, sizeof(mi));
	mi.cbSize = sizeof(mi);

	// load options
	bVCardEnabled = DBGetByte(SET_VCARDMENUITEMS_ENABLED, TRUE);

	// is genmenu installed and cascading contact menues possible?
	if(ServiceExists(MS_CLIST_REMOVEMAINMENUITEM)) {
		static BOOLEAN bOldCascade = MCAS_NOTINITIATED; 
		
		bCascade = bVCardEnabled ? DBGetByte(SET_CONTACTMENU_CASCADE, MCAS_ALL) : MCAS_DISABLED;
		if(bCascade != bOldCascade) {
			bOldCascade = bCascade;
			if(mhRoot) {
				if(mhRoot != -1) CallService(MS_CLIST_REMOVECONTACTMENUITEM, mhRoot, NULL);
				mhRoot = NULL;
			}
			bRootChanged = TRUE;		
			// add root menuitem for all the other stuff
			if(bCascade > MCAS_DISABLED) {
				if(bCascade == MCAS_ALL) {
					mi.position = 1000050000;
					mi.hIcon = (HICON)LoadImage(ghInst, MAKEINTRESOURCE(IDI_MAIN), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), 0);
					mi.pszName = MODULELONGNAME;
				}
				else {
					mi.position = 1000050100;
					mi.hIcon = NIcoLib::GetIcon(ICO_BTN_EXIMPORT, IDI_BTN_EXIMPORT);
					mi.pszName = "Ex-/Import contact";
				}
				mi.pszPopupName = (LPSTR)(-1);
				mi.flags = CMIF_ROOTPOPUP;
				mhRoot = CallService(MS_CLIST_ADDCONTACTMENUITEM, 0, (LPARAM)&mi);
			}
		}
	}
	// avoid building menu twice if removing items is not supported
	else if(mhRoot != -1)
		return;
	// simulate root changed to make rebuild working correctly in any case
	else {
		mhRoot = 0;
		bRootChanged = TRUE;
	}
	
	// ContactDetailsPS's menuitem
	if(bRootChanged) {
		static INT hCdPsMenuItem = NULL;

		if(hCdPsMenuItem != NULL) CallService(MS_CLIST_REMOVECONTACTMENUITEM, hCdPsMenuItem, NULL);
		if(bCascade == MCAS_ALL) { mi.pszPopupName = (LPSTR)mhRoot; mi.flags = CMIF_CHILDPOPUP; }
		else { mi.pszPopupName = 0;	mi.flags = 0; }
		mi.hIcon = (HICON)LoadImage(ghInst, MAKEINTRESOURCE(IDI_MAIN), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), 0);
		mi.pszService = MS_USERINFO_SHOWDIALOG;
		mi.hotKey = MAKELPARAM(VK_F3, MOD_ALT);
		mi.position = 1000050000;
		mi.pszName = "User &Details";
		hCdPsMenuItem = CallService(MS_CLIST_ADDCONTACTMENUITEM, 0, (LPARAM)&mi);
	}

	// VCard's menuitems
	{
		static INT ghContactMenuItem[2] = { NULL, NULL };
		BYTE i;

		if(bRootChanged || !bVCardEnabled) {
			for(i = 0; i < SIZEOF(ghContactMenuItem); i++) {
				if(ghContactMenuItem[i]) {
					CallService(MS_CLIST_REMOVECONTACTMENUITEM, (WPARAM)ghContactMenuItem[i], 0);
					ghContactMenuItem[i] = NULL;
				}
			}
			if(bVCardEnabled) {
				if(bCascade > MCAS_DISABLED) { mi.pszPopupName = (LPSTR)mhRoot; mi.flags = CMIF_CHILDPOPUP; }
				else { mi.pszPopupName = 0;	mi.flags = 0; }
				i = 0;
				mi.position = 1000050200;
				mi.hIcon = NIcoLib::GetIcon(ICO_BTN_IMPORT);
				mi.pszName = mhRoot ? "&Import" : "&Import User Details";
				mi.pszService = MS_USERINFO_VCARD_IMPORT;
				ghContactMenuItem[i++] = CallService(MS_CLIST_ADDCONTACTMENUITEM, 0, (LPARAM)&mi);
				mi.position = 1000050300;
				mi.hIcon = NIcoLib::GetIcon(ICO_BTN_EXPORT);
				mi.pszName = mhRoot ? "&Export" : "&Export User Details";
				mi.pszService = MS_USERINFO_VCARD_EXPORT;
				ghContactMenuItem[i++] = CallService(MS_CLIST_ADDCONTACTMENUITEM, 0, (LPARAM)&mi);
			}
		}
	}
}

/**
 * name:	RebuildMain
 * desc:	This function rebuilds the mainmenu. If genmenu is installed and user selected to
 *			cascade menus, a root menu item is created which holds the popup for all the other items.
 * params:	none
 * return:	nothing
 **/
VOID RebuildMain()
{
	static INT mhRoot = -1;
	BOOLEAN bRootChanged = FALSE;
	CLISTMENUITEM mi;

	// set all bytes 0 to avoid problems
	ZeroMemory(&mi, sizeof(mi));
	mi.cbSize = sizeof(mi);

	if(ServiceExists(MS_CLIST_REMOVEMAINMENUITEM)) {
		// add root menuitem for all the other stuff
		if(DBGetByte(SET_MENU_CASCADE, TRUE)) {
			if(mhRoot == NULL || mhRoot == -1) {
				mi.pszPopupName = (char*)-1;
				mi.flags = CMIF_ROOTPOPUP;
				mi.position = 500050000;
				mi.hIcon = (HICON)LoadImage(ghInst, MAKEINTRESOURCE(IDI_MAIN), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), 0);
				mi.pszName = MODULELONGNAME;
				mhRoot = CallService(MS_CLIST_ADDMAINMENUITEM, 0, (LPARAM)&mi);
				bRootChanged = TRUE;
			}
			mi.pszPopupName = (LPSTR)mhRoot;
			mi.flags = CMIF_CHILDPOPUP;
		}
		// remove root menuitem
		else if(mhRoot != NULL) {
			if(mhRoot != -1) CallService(MS_CLIST_REMOVEMAINMENUITEM, mhRoot, NULL);
			mhRoot = NULL;
			bRootChanged = TRUE;
		}
	}
	// avoid building menu twice if removing items is not supported
	else if(mhRoot != -1)
		return;
	// simulate root changed to make rebuild working correctly in any case
	else {
		mhRoot = 0;
		bRootChanged = TRUE;

		// use old variant of cascading if no genmenu is installed
		if(DBGetByte(SET_MENU_CASCADE, TRUE))
			mi.pszPopupName = MODULELONGNAME;
	}

	// details dialog
	if(bRootChanged) {
		static INT hMenuItemDetails = NULL;

		if(hMenuItemDetails != NULL) CallService(MS_CLIST_REMOVEMAINMENUITEM, hMenuItemDetails, NULL);
		mi.hIcon = (HICON)LoadImage(ghInst, MAKEINTRESOURCE(IDI_MAIN), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), 0);
		mi.position = 500050000;
		mi.pszName = "View/Change My &Details...";
		mi.pszService = MS_USERINFO_SHOWDIALOG;
		hMenuItemDetails = CallService(MS_CLIST_ADDMAINMENUITEM, 0, (LPARAM)&mi);
	}

	// vCard
	if(bRootChanged) {
		static INT hMenuItemExport = NULL;
		static INT hMenuItemImport = NULL;

		if(hMenuItemExport != NULL) CallService(MS_CLIST_REMOVEMAINMENUITEM, hMenuItemExport, NULL);
		mi.hIcon = NIcoLib::GetIcon(ICO_BTN_EXPORT);
		mi.position = 500150000;
		mi.pszName = "Export all contacts";
		mi.pszService = MS_USERINFO_VCARD_EXPORTALL;
		hMenuItemExport = CallService(MS_CLIST_ADDMAINMENUITEM, 0, (LPARAM)&mi);
		
		if(hMenuItemImport != NULL) CallService(MS_CLIST_REMOVEMAINMENUITEM, hMenuItemImport, NULL);
		mi.hIcon = NIcoLib::GetIcon(ICO_BTN_IMPORT);
		mi.position = 500151000;
		mi.pszName = "Import all contacts";
		mi.pszService = MS_USERINFO_VCARD_IMPORTALL;
		hMenuItemImport = CallService(MS_CLIST_ADDMAINMENUITEM, 0, (LPARAM)&mi);
	}

	// reminder
	{
		static INT hMenuItemBackup = NULL;
		static INT hMenuItemCheck = NULL;
		const BOOLEAN bRemindMenus =
			DBGetByte(SET_REMIND_ENABLED, DEFVAL_REMIND_ENABLED) &&
			DBGetByte(SET_REMIND_MENUENABLED, DEFVAL_REMIND_MENUENABLED);

		if(bRootChanged || !bRemindMenus) {
			if(hMenuItemBackup != NULL) CallService(MS_CLIST_REMOVEMAINMENUITEM, hMenuItemBackup, NULL);
			if(hMenuItemCheck != NULL) CallService(MS_CLIST_REMOVEMAINMENUITEM, hMenuItemCheck, NULL);
			
			if(bRemindMenus) {
				mi.hIcon = NIcoLib::GetIcon(ICO_BTN_BDAY_BACKUP);
				mi.position = 500253000;
				mi.pszName = "Backup birthdays";
				mi.pszService = MS_USERINFO_REMINDER_AGGRASIVEBACKUP;
				hMenuItemBackup = CallService(MS_CLIST_ADDMAINMENUITEM, 0, (LPARAM)&mi);

				mi.hIcon = NIcoLib::GetIcon(ICO_BTN_SEARCH);
				mi.position = 500251000;
				mi.pszName = "Check anniversaries";
				mi.pszService = MS_USERINFO_REMINDER_CHECK;
				hMenuItemCheck = CallService(MS_CLIST_ADDMAINMENUITEM, 0, (LPARAM)&mi);
			}
		}
	}

	// anniversary list
	if(bRootChanged) {
		static INT hMenuItemAnnivList = NULL;

		if(hMenuItemAnnivList != NULL) CallService(MS_CLIST_REMOVEMAINMENUITEM, hMenuItemAnnivList, NULL);
		mi.hIcon = NIcoLib::GetIcon(ICO_COMMON_ANNIVERSARY, IDI_ANNIVERSARY);
		mi.position = 500252000;
		mi.pszName = "Anniversary list";
		mi.pszService = MS_USERINFO_REMINDER_LIST;
		hMenuItemAnnivList = CallService(MS_CLIST_ADDMAINMENUITEM, 0, (LPARAM)&mi);
	}
}

/**
 * name:	MI_RebuildGroupMenu
 * desc:	This function rebuilds the mainmenu. If genmenu is installed and user selected to
 *			cascade menus, a root menu item is created which holds the popup for all the other items.
 * params:	none
 * return:	nothing
 **/
VOID RebuildGroup()
{
/*	CLISTMENUITEM mi;
	GroupMenuParam param = { 0, 0 };

	ZeroMemory(&mi,sizeof(mi));
	mi.cbSize = sizeof(mi);

	CreateServiceFunction("UserInfo/ExportGroup", exportGroup);

	// vCard
	{
		mi.hIcon = NIcoLib::GetIcon(ICO_BTN_EXPORT, IDI_BTN_EXPORT);
		mi.position = 500252000;
		mi.pszName = "&Export group";
		mi.pszService = "UserInfo/ExportGroup";
		CallService(MS_CLIST_ADDSUBGROUPMENUITEM, (WPARAM)&param, (LPARAM)&mi);
	}
*/
}

} // namespace MenuItems